package com.yuan.model_app.service.impl;

import com.yuan.model_app.rest.resp.PaymentInfoResp;
import com.yuan.model_app.rest.resp.UserHouseInfoResp;
import com.yuan.model_app.service.UserAppService;
import com.yuan.model_community.domain.CommunityBuilding;
import com.yuan.model_community.domain.CommunityHouse;
import com.yuan.model_community.domain.CommunityParking;
import com.yuan.model_community.domain.CommunityUnit;
import com.yuan.model_community.repository.CommunityBuildingDao;
import com.yuan.model_community.repository.CommunityHouseDao;
import com.yuan.model_community.repository.CommunityParkingDao;
import com.yuan.model_community.repository.CommunityUnitDao;
import com.yuan.model_community.service.dto.CommunityBuildingDto;
import com.yuan.model_community.service.dto.CommunityHouseDto;
import com.yuan.model_community.service.dto.CommunityParkingDto;
import com.yuan.model_community.service.dto.CommunityUnitDto;
import com.yuan.model_community.service.mapstruct.CommunityBuildingMapper;
import com.yuan.model_community.service.mapstruct.CommunityHouseMapper;
import com.yuan.model_community.service.mapstruct.CommunityParkingMapper;
import com.yuan.model_community.service.mapstruct.CommunityUnitMapper;
import com.yuan.model_payment.service.PaymentBillService;
import com.yuan.model_payment.service.dto.PaymentBillDto;
import com.yuan.model_payment.service.dto.PaymentBillQueryCriteria;
import lombok.RequiredArgsConstructor;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class UserUserAppServiceImpl implements UserAppService {

    private final CommunityBuildingDao communityBuildingDao;

    private final CommunityUnitDao communityUnitDao;

    private final CommunityHouseDao communityHouseDao;

    private final CommunityParkingDao communityParkingDao;

    private final CommunityBuildingMapper communityBuildingMapper;
    private final CommunityUnitMapper communityUnitMapper;
    private final CommunityHouseMapper communityHouseMapper;
    private final CommunityParkingMapper communityParkingMapper;

    private final PaymentBillService paymentBillService;

    @Override
    public List<PaymentInfoResp> paymentCountInfo(Long userId, Pageable pageable){
        List<PaymentInfoResp> paymentInfoResps = new ArrayList<>();
        PaymentBillQueryCriteria paymentBillQueryCriteria = new PaymentBillQueryCriteria();
        paymentBillQueryCriteria.setLiverId(userId);
        PageResult<PaymentBillDto> paymentBillDtoPageResult = paymentBillService.queryAll(paymentBillQueryCriteria, pageable);
        for (PaymentBillDto paymentBillDto : paymentBillDtoPageResult.getContent()) {
            PaymentInfoResp paymentInfoResp = new PaymentInfoResp();
            paymentInfoResp.setMonth(paymentBillDto.getPayMonth());
            paymentInfoResp.setType(paymentInfoResp.getType());
            paymentInfoResp.setMoney(paymentBillDto.getPayActualMoney());
            paymentInfoResps.add(paymentInfoResp);
        }
        return paymentInfoResps;
    }

    @Override
    public UserHouseInfoResp userHouseInfo(Long userId){
        UserHouseInfoResp userHouseInfoResp = new UserHouseInfoResp();
        CommunityHouse houseByUserId = communityHouseDao.findHouseByUserId(userId);
        if(houseByUserId != null){
            CommunityHouseDto dto = communityHouseMapper.toDto(houseByUserId);
            userHouseInfoResp.setCommunityHouseDto(dto);
            Optional<CommunityUnit> byId = communityUnitDao.findById(houseByUserId.getUnitId());
            if (byId.isPresent()) {
                CommunityUnit communityUnit = byId.get();
                CommunityUnitDto dto1 = communityUnitMapper.toDto(communityUnit);
                userHouseInfoResp.setCommunityUnitDto(dto1);
                Optional<CommunityBuilding> byId1 = communityBuildingDao.findById(communityUnit.getBuildingId());
                if (byId1.isPresent()) {
                    CommunityBuilding communityBuilding = byId1.get();
                    CommunityBuildingDto dto2 = communityBuildingMapper.toDto(communityBuilding);
                    userHouseInfoResp.setCommunityBuildingDto(dto2);
                    List<CommunityParking> parkingByUserId = communityParkingDao.findParkingByUserId(userId);
                    if(!parkingByUserId.isEmpty()){
                        CommunityParking communityParking = parkingByUserId.get(0);
                        CommunityParkingDto dto3 = communityParkingMapper.toDto(communityParking);
                        userHouseInfoResp.setCommunityParkingDto(dto3);
                    }
                }
            }
        }
        return userHouseInfoResp;
    }
}
